package com.dungnt.vtime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.dungnt.vtime.utils.Constant;

public class VTSetCountdownTime extends Activity implements OnClickListener,
		OnSeekBarChangeListener {
	private TextView tvTime, tvHour, tvMin, tvSec;
	private Button btnCancel, btnSet;
	private SeekBar sbHour, sbMin, sbSec;
	private int hour, min, sec;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settimecountdown_activity);
		setTitle(getString(R.string.set_time_title));
		tvTime = (TextView) findViewById(R.id.set_time_Time);
		tvHour = (TextView) findViewById(R.id.set_time_hours);
		tvMin = (TextView) findViewById(R.id.set_time_mins);
		tvSec = (TextView) findViewById(R.id.set_time_secs);
		btnCancel = (Button) findViewById(R.id.set_time_btnCancel);
		btnSet = (Button) findViewById(R.id.set_time_btnSet);
		sbHour = (SeekBar) findViewById(R.id.sbHour);
		sbMin = (SeekBar) findViewById(R.id.sbMins);
		sbSec = (SeekBar) findViewById(R.id.sbSecs);
		sbHour.setMax(99);
		sbMin.setMax(59);
		sbSec.setMax(59);
		sbHour.setOnSeekBarChangeListener(this);
		sbMin.setOnSeekBarChangeListener(this);
		sbSec.setOnSeekBarChangeListener(this);
		btnCancel.setOnClickListener(this);
		btnSet.setOnClickListener(this);
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub

		super.finish();
	}

	private void updateTime() {
		tvTime.setText("" + tvHour.getText().toString() + ":"
				+ tvMin.getText().toString() + ":" + tvSec.getText().toString());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btnCancel) {
			finish();
		} else if (v == btnSet) {
			setTime(hour, min, sec);
			finish();
		}
	}

	private void setTime(int hour, int min, int sec) {
		Intent data = new Intent();
		data.putExtra(Constant.HOURS, hour);
		data.putExtra(Constant.MINUTES, min);
		data.putExtra(Constant.SECONDS, sec);
		setResult(RESULT_OK, data);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		if (seekBar == sbHour) {
			if (progress < 10) {
				tvHour.setText("0" + progress);
			} else {
				tvHour.setText("" + progress);
			}
			hour = progress;

		} else if (seekBar == sbMin) {
			if (progress < 10) {
				tvMin.setText("0" + progress);
			} else {
				tvMin.setText("" + progress);
			}
			min = progress;
		} else if (seekBar == sbSec) {
			if (progress < 10) {
				tvSec.setText("0" + progress);
			} else {
				tvSec.setText("" + progress);
			}
			sec = progress;
		}
		updateTime();
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}
}
